package com.vsc.print_service.controller;

import com.vsc.print_service.enums.FileTypes;
import com.vsc.print_service.enums.Printers;
import com.vsc.print_service.qo.PrintQo;
import com.vsc.print_service.util.ApiException;
import com.vsc.print_service.util.MessageCode;
import com.vsc.print_service.util.PrintUtil;
import com.vsc.print_service.util.ResultObject;
import com.vsc.print_service.vo.BaseVo;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

@RestController
@RequestMapping("/printer")
public class PrintController {


    @PostMapping(value = "/print/url/{fileType}")
    public ResultObject<BaseVo> printUrl(
            HttpServletRequest request,
            @PathVariable String fileType,
            @Valid @RequestBody PrintQo qo,
            BindingResult errors
    ) throws Exception {
        if (errors.hasErrors()) {
            return new ResultObject(MessageCode.CODE_PARAMETER_ERROR,
                    errors.getAllErrors());
        }
        FileTypes type = FileTypes.valueOf(fileType.toUpperCase());
        boolean isSuccess = false;
        switch (type){
            case PDF:
                isSuccess = PrintUtil.PDFPrint(PrintUtil.getNetUrlHttp(qo.getUrl()), Printers.DEFAULT.getPrinterName());
                break;
            case IMAGE:
                isSuccess = PrintUtil.imagePrint(PrintUtil.getNetUrlHttp(qo.getUrl()), Printers.DEFAULT.getPrinterName());
                break;
            default:
                throw new ApiException(MessageCode.CODE_PARAMETER_ERROR);
        }
        return new ResultObject<>(MessageCode.CODE_SUCCESS, new BaseVo(isSuccess));
    }


    @PostMapping(value = "/print/file/{fileType}")
    public ResultObject<BaseVo> printFile(
            HttpServletRequest request,
            @PathVariable String fileType,
            MultipartFile multipartFile
    ) throws Exception {
        if (multipartFile.isEmpty()) {
            throw new ApiException(MessageCode.CODE_PARAMETER_ERROR);
        }
        FileTypes type = FileTypes.valueOf(fileType.toUpperCase());
        boolean isSuccess = false;
        byte [] byteArr = multipartFile.getBytes();
        InputStream inputStream = new ByteArrayInputStream(byteArr);
        switch (type){
            case PDF:
                isSuccess = PrintUtil.PDFPrint(inputStream, Printers.DEFAULT.getPrinterName());
                break;
            case IMAGE:
                isSuccess = PrintUtil.imagePrint(inputStream, Printers.DEFAULT.getPrinterName());
                break;
            default:
                throw new ApiException(MessageCode.CODE_PARAMETER_ERROR);
        }
        return new ResultObject<>(MessageCode.CODE_SUCCESS, new BaseVo(isSuccess));
    }


    @PostMapping(value = "/test")
    public ResultObject<BaseVo> test(
            HttpServletRequest request
    ) throws Exception {
//        if (errors.hasErrors()) {
//            return new ResultObject(MessageCode.CODE_PARAMETER_ERROR,
//                    errors.getAllErrors());
//        }
//        FileTypes type = FileTypes.valueOf(fileType.toUpperCase());
//        boolean isSuccess = false;
//        switch (type){
//            case PDF:
//                isSuccess = PrintUtil.PDFPrint(PrintUtil.getNetUrlHttp(qo.getUrl()), Printers.DEFAULT.getPrinterName());
//                break;
//            case IMAGE:
//                isSuccess = PrintUtil.ImagePrint(PrintUtil.getNetUrlHttp(qo.getUrl()), Printers.DEFAULT.getPrinterName());
//                break;
//            default:
//                throw new ApiException(MessageCode.CODE_PARAMETER_ERROR);
//        }
        System.out.println("可以调");
        return new ResultObject<>(MessageCode.CODE_SUCCESS, new BaseVo(true));
    }
}